package com.cfilmcloud.learn.demo;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.temporal.ChronoUnit;
import java.util.Date;

/**
 *  Description: 日期<br>
 * 1、…<br>
 * 2、…<br>
 * Implement: <br>
 * 1、…<br>
 * 2、…<br>
 * 
 * @author mengxy[孟祥元][mengxiangyuan@cfilmcloud.com]
 * Created by edz on 2017/7/19.
 */
public class DemoDate {

    private static ThreadLocal<SimpleDateFormat> DateFormaterHolder = ThreadLocal
            .withInitial(() -> new SimpleDateFormat("yyyy-MM-dd"));
    private static ThreadLocal<SimpleDateFormat> TimeFormaterHolder = ThreadLocal
            .withInitial(() -> new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));

    public static void main(String[] args) {
        {
            String text = "2017-09-09 12:00:00";
            String date1 = DateFormaterHolder.get().format(new Date());
            String date2 = TimeFormaterHolder.get().format(new Date());
            System.err.println("" + date1);
            System.err.println("" + date2);
            try {
                Date date3 = TimeFormaterHolder.get().parse(text);
                System.err.println("" + date3);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        {
            //LocalDateTime 转 Date
            LocalDateTime localDateTime = LocalDateTime.now();
            Date date4 = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
            System.err.println("" + date4);
        }

        {
            //Date 转 LocalDateTime
            Date date5 = new Date();
            LocalDateTime localDateTime = Instant.ofEpochMilli(date5.getTime()).atZone(ZoneId.systemDefault())
                    .toLocalDateTime();
            System.err.println("" + localDateTime);
        }

        {
            // Instant: 时间戳
            // Duration: Instant 之间的时间差
            // Period: LocalDateTime 之间的时间差
            Instant begin = Instant.now();
            begin = begin.plus(5, ChronoUnit.SECONDS);
            begin.isBefore(Instant.now());
            begin.toEpochMilli();

            Instant end = Instant.now();
            Duration duration = Duration.between(end, begin);
        }
    }
}
